package com.huilunyingshi.videoprovider.serviceImp;

import com.alibaba.dubbo.config.annotation.Service;
import com.huilunyingshi.videoapi.service.TAnimeService;
import com.huilunyingshi.videoprovider.dao.TAnimeMapper;
import com.huilunyingshi.videoprovider.dao.TLableMapper;
import com.huilunyingshi.videoprovider.entity.TAnime;
import com.huilunyingshi.videoprovider.entity.TLable;
import com.hullunyingshi.until.ControllerResult;
import com.hullunyingshi.until.PageHelper;
import com.hullunyingshi.until.StringTools;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
@Slf4j
public class AnimeServiceImp implements TAnimeService {
    private final Logger logger = LoggerFactory.getLogger(AnimeServiceImp.class);

    @Autowired
    private TAnimeMapper tAnimeMapper;

    @Autowired
    private TLableMapper tLableMapper;

    /**
     * 查询所有动漫
     * @param keyword
     * @param curPage
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public ControllerResult getAnimeList(String keyword, Integer curPage) {
        try {
            if (StringTools.strHasNoData(keyword)){
                keyword = "";
            }
            if (StringTools.strHasNoData(curPage)){
                curPage = 1;
            }
            Integer pageSize = 20;
            int totalCount = tAnimeMapper.getAnimeListCount();
            PageHelper pageHelper = new PageHelper(curPage,pageSize,totalCount);
            Integer totalPage = pageHelper.getTotalPage();
            Integer start = PageHelper.getStartIndex(curPage, pageSize);
            ArrayList<TAnime> animeList = tAnimeMapper.getAnimeList(keyword, start, pageSize);
            pageHelper.setData(animeList);
            return new ControllerResult("200","查询动漫列表成功",pageHelper);
        }catch (Exception e){
            e.printStackTrace();
            throw new RuntimeException(e.toString());
        }

    }

    /**
     * 查询动漫详情
     * @param id
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public ControllerResult getAnime(Long id) {
        try {
            if (StringTools.strHasNoData(id)){
                return new ControllerResult("403","动漫id不能为空",null);
            }
            TAnime anime=tAnimeMapper.selectByPrimaryKey(id);
            return new ControllerResult("200","点击动漫显示页面成功",anime);


        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(e.toString());
        }

    }

    /**
     * 根据类型查询动漫列表
     * @param lid
     * @param curPage
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public ControllerResult getAnimeTypeList(Long lid, Integer curPage) {
        try {
            if (StringTools.strHasNoData(lid)) {
                return new ControllerResult("403", "类型id不能为空", null);
            }
            if (StringTools.strHasNoData(curPage)) {
                curPage = 1;
            }
            Integer pageSize = 20;
            int totalCount = tAnimeMapper.getAnimeListCount();
            PageHelper pageHelper = new PageHelper(curPage, pageSize, totalCount);
            Integer startIndex = PageHelper.getStartIndex(curPage, pageSize);
            List<Map<String, Object>> AnimeTypeList = tAnimeMapper.getAnimeTypeList(lid, startIndex, pageSize);
            pageHelper.setData(AnimeTypeList);


            return new ControllerResult("200", "查询type动漫列表成功", pageHelper);
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(e.toString());
        }


    }

    /**
     * 多条件筛选查询
     * @param typeName 动漫类型名称 （热血）
     * @param animeReleaseTime 动漫年代
     * @param animeArea  动漫地区
     * @param animeVersion 动漫版本 （tv版，剧场版，ova版，真人版）
     * @param animeStatus 动漫状态（连载，完结）
     * @param curPage 当前页
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public ControllerResult getAnimeTypeLists(String typeName, String animeReleaseTime, String animeArea,String animeVersion, String animeStatus,Integer curPage) {
        try {
            if (StringTools.strHasNoData(curPage)) {
                curPage = 1;
            }
            Long lid = null;
            Long artId = null;
            Long areaId = null;
            Long avId = null;
            Long asId = null;
            if (!StringTools.strHasNoData(typeName)) {
                TLable tLable = tLableMapper.selectByTypeName(typeName,3);
                lid = tLable.getId();
            }
            if (!StringTools.strHasNoData(animeReleaseTime)) {
                TLable tLable = tLableMapper.selectByTypeName(animeReleaseTime,3);
                artId = tLable.getId();
            }
            if (!StringTools.strHasNoData(animeArea)) {
                TLable tLable = tLableMapper.selectByTypeName(animeArea,3);
                areaId = tLable.getId();
            }
            if (!StringTools.strHasNoData(animeVersion)) {
                TLable tLable = tLableMapper.selectByTypeName(animeVersion,3);
                avId = tLable.getId();
            }
            if (!StringTools.strHasNoData(animeStatus)) {
                TLable tLable = tLableMapper.selectByTypeName(animeStatus,3);
                asId = tLable.getId();
            }


            Integer pageSize = 20;
            int totalCount = tAnimeMapper.getAnimeListCount();
            PageHelper pageHelper = new PageHelper(curPage, pageSize, totalCount);
            Integer startIndex = PageHelper.getStartIndex(curPage, pageSize);
            List<Map<String, Object>> animeTypeLists = tAnimeMapper.getAnimeTypeLists(lid, artId, areaId,avId,asId, startIndex, pageSize);
            pageHelper.setData(animeTypeLists);




            return new ControllerResult("200", "查询typefilms列表成功", pageHelper);
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(e.toString());
        }
    }
}
